﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentScheduler;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.EFDBFirst;
using System.Text.RegularExpressions;

namespace YiYiTong.Rocket.ProcessConsole
{
    /// <summary>
    /// 会员等级调整
    /// </summary>
    public class MemberJob : BaseJob
    {
        public override void DoAction()
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            var users = dbcontext.ADZ_MemberMaster.Where(t => t.member_level == 20).ToList();
            foreach (var item in users)
            {
                var log = dbcontext.ADZ_MemberLevelLog.OrderByDescending(t => t.id).FirstOrDefault(t => t.uid == item.uid);
                if (log != null && log.create_time.Value.AddYears(1) < DateTime.Now)
                {
                    //执行降级
                    var aDZ_MemberLevelConfig = dbcontext.ADZ_MemberLevelConfig.OrderByDescending(t => t.member_level).FirstOrDefault(t => t.member_level < 20);
                    if (aDZ_MemberLevelConfig == null)
                    {
                        continue;
                    }

                    dbcontext.ADZ_MemberLevelLog.Add(new ADZ_MemberLevelLog
                    {
                        create_time = DateTime.Now,
                        uid = item.uid,
                        old_level = item.member_level,
                        new_level = aDZ_MemberLevelConfig.member_level,
                        msg = $"等级变为{aDZ_MemberLevelConfig.member_name}",
                        opuser = "systemauto"
                    });

                    item.member_level = (byte)aDZ_MemberLevelConfig.member_level;
                    //等级提升昵称一并修改，等级名称+手机号格式 by leev 2019-03-13
                    Regex reg = new Regex(@"^[\u4E00-\u9FA5]{4}[0-9]{11}$");
                    //4个汉字+11位数字匹配
                    if (reg.IsMatch(item.member_name))
                        item.member_name = aDZ_MemberLevelConfig.member_name.Substring(0, 4) + item.account_mobile;
                    dbcontext.SaveChanges();


                    //提升会员等级后清除token，让用户重新登录
                    OAuthBiz.Instance.RemoveToken(item.uid);

                    //清除邀请码
                    dbcontext.ADZ_InviteCode.RemoveRange(dbcontext.ADZ_InviteCode.Where(t => t.uid == item.uid && t.is_used == 0));
                    dbcontext.SaveChanges();

                    AddPushMsgModel paramModel = new AddPushMsgModel
                    {
                        aDZ_Pushmessage = new ADZ_Pushmessage
                        {
                            MsgType = 2,
                            ReceiveType = 1
                        },
                        ReceiveUids = item.uid.ToString()
                    };
                    //发送推送消息
                    paramModel.aDZ_Pushmessage.MsgTitle = "会员降级成功";
                    paramModel.aDZ_Pushmessage.MsgText = $"您的会员等级已经被降为{aDZ_MemberLevelConfig.member_name},如有疑问请联系客服。";
                    PushMsgBiz.Instance.AddPushMsg(paramModel);
                }
            }
        }
    }
}